package com.lanxin.oa.personnel.controller;

import com.lanxin.bean.*;
import com.lanxin.oa.personnel.service.EmpService;
import com.lanxin.util.PageBean;
import com.lanxin.util.TimeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/emp")
public class EmpController extends TimeConverter {

    @Autowired
    private EmpService empService;

    @RequestMapping("/listEmp")
    public ModelAndView listEmp(Integer page, Integer pageSize, ZzEmployee emp) {
        PageBean<ZzEmployee> pageBean = empService.getEmpPage(page, pageSize, emp);
        ModelAndView mv = new ModelAndView();
        List<ZzDepartment> listDept = empService.listDeptName();
        List<JcBasicdatavalue> listJob = empService.selectJobs();
        List<JcBasicdatavalue> listDegree = empService.selectEducations();
        mv.addObject("pageBean", pageBean);
        mv.addObject("emp", emp);
        mv.addObject("listDept", listDept);
        mv.addObject("listJob", listJob);
        mv.addObject("listDegree", listDegree);
        mv.setViewName("../view/personnel/listemp.jsp");
        return mv;
    }

    @RequestMapping("/empInfo")
    public ModelAndView empInfo(Integer oid) {
        ModelAndView mv = new ModelAndView();
        ZzEmployee emp = empService.empById(oid);
        mv.addObject("emp", emp);
        mv.setViewName("../view/personnel/empinfo.jsp");
        return mv;
    }

    @RequestMapping("/toAddEmp")
    public ModelAndView toAddEmp() {
        ModelAndView mv = new ModelAndView();
        List<ZzDepartment> listDept = empService.listDeptName();
        List<JcBasicdatavalue> listDegree = empService.selectEducations();
        mv.addObject("listDept", listDept);
        mv.addObject("listDegree", listDegree);
        mv.setViewName("../view/personnel/editemp.jsp");
        return mv;
    }

    @RequestMapping("/editEmp")
    public ModelAndView editEmp(ZzEmployee emp, ZzAccount account, ZzJobinfo jobInfo, Date[] startDate, Date[] endDate, String[] companyName, String[] deptName, String[] jobName, String[] proofer, String[] phone, String[] description) {
        ModelAndView mv = new ModelAndView();
        empService.editemp(emp,account,jobInfo,startDate,endDate,companyName,deptName,jobName,proofer,phone,description);
        mv.setViewName("redirect:/emp/listEmp");// 重定向到查询方法里
        return mv;
    }


    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String,Boolean> checkUsername(String username, Integer aid){
        System.out.println(username+"  "+aid);
        Map<String,Boolean> map=new HashMap<String, Boolean>();
        boolean b=empService.selectByUserNameService(username,aid);
        if(!b){
            map.put("valid", true);
            return map;
        }
        map.put("valid", false);
        return map;
    }

    @RequestMapping("/toUpdateEmp")
    public ModelAndView toUpdateEmp(Integer oid){
        ModelAndView mv=new ModelAndView();
        ZzEmployee emp = empService.empById(oid);
        List<ZzDepartment> listDept = empService.listDeptName();
        List<JcBasicdatavalue> listDegree = empService.selectEducations();
        mv.addObject("listDept",listDept);
        mv.addObject("listDegree",listDegree);
        mv.addObject("emp",emp);
        mv.setViewName("../view/personnel/editemp.jsp");
        return mv;
    }

    @RequestMapping("/deleteEmp")
    @ResponseBody
    public boolean deleteEmp(Integer  oid){

        boolean b=empService.deleteEmp(oid);
        return b;
    }

    @RequestMapping("/updateEmpStatue")
    @ResponseBody
    public boolean updateEmpStatue(Integer  oid,String state){

        boolean b=empService.updateEmpStatue(oid,state);
        return b;
    }
}
